package pe.edu.upc.dsd.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import pe.edu.upc.dsd.dominio.Hambur;

@Service("hamburService")
public class HamburService {

	protected static Logger logger = Logger.getLogger("service");

	// In-memory list
	private List<Hambur> HAMBU = new ArrayList<Hambur>();

	public HamburService() {
		logger.debug("Init database");

		// Create in-memory list
		Hambur sandwich1 = new Hambur();
		sandwich1.setid(1l);
		sandwich1.setNombre("WAPER EXTREMO");
		sandwich1.setImgrediente1("AROS DE CEBOLLA, TOMATE, LECHUGA");
		sandwich1.setImgrediente1("TOCINO, HUEVO, QUESO");
		sandwich1.setTipo("CARNE");
		sandwich1.setCategoria("JUNIOR");
		sandwich1.setPrecio("15");
		sandwich1.setDescripcion("CON PAPAS Y GASEOSA");

		Hambur sandwich2 = new Hambur();
		sandwich2.setid(2l);
		sandwich2.setNombre("WAPER LIGTH");
		sandwich2.setImgrediente1("AROS DE CEBOLLA, TOMATE, LECHUGA");
		sandwich2.setImgrediente1("QUESO");
		sandwich2.setTipo("CARNE");
		sandwich2.setCategoria("JUNIOR");
		sandwich2.setPrecio("19");
		sandwich2.setDescripcion("CON ENSALADA Y GASEOSA");

		Hambur sandwich3 = new Hambur();
		sandwich3.setid(3l);
		sandwich3.setNombre("WAPER HOT");
		sandwich3.setImgrediente1("AROS DE CEBOLLA, TOMATE, LECHUGA");
		sandwich3.setImgrediente1("QUESO, CHILE");
		sandwich3.setTipo("CARNE");
		sandwich3.setCategoria("JUNIOR");
		sandwich3.setPrecio("19");
		sandwich3.setDescripcion("CON ENSALADA Y GASEOSA");

		HAMBU.add(sandwich1);
		HAMBU.add(sandwich2);
		HAMBU.add(sandwich3);

	}

	/**
	 * Retrieves all persons
	 */
	public List<Hambur> getAll() {
		logger.debug("Retrieving all persons");

		return HAMBU;
	}

	/**
	 * Retrieves a single person
	 */
	public Hambur get(Long id) {
		logger.debug("Retrieving person with id: " + id);

		for (Hambur person : HAMBU) {
			if (person.getid().longValue() == id.longValue()) {
				logger.debug("Found record");
				return person;
			}
		}

		logger.debug("No records found");
		return null;
	}

	/**
	 * Adds a new person
	 */
	public Hambur add(Hambur hamb) {
		logger.debug("Adding new person");

		try {
			// Find suitable id
			Long newId = 0L;
			Boolean hasFoundSuitableId = false;
			while (hasFoundSuitableId == false) {
				for (int i = 0; i < HAMBU.size(); i++) {
					if (HAMBU.get(i).getid().longValue() == newId
							.longValue()) {
						newId++;
						break;
					}

					// Exit while loop
					if (i == HAMBU.size() - 1) {
						logger.debug("Assigning id: " + newId);
						hasFoundSuitableId = true;
					}
				}
			}

			// Assign new id
			hamb.setid(newId);
			// Add to list
			HAMBU.add(hamb);

			logger.debug("Added new chofer");
			return hamb;
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}

	/**
	 * Deletes an existing person
	 */
	public Boolean delete(Long id) {
		logger.debug("Deleting person with id: " + id);

		try {
			for (Hambur person : HAMBU) {
				if (person.getid().longValue() == id.longValue()) {
					logger.debug("Found record");
					HAMBU.remove(person);
					return true;
				}
			}

			logger.debug("No records found");
			return false;

		} catch (Exception e) {
			logger.error(e);
			return false;
		}
	}

	/**
	 * Edits an existing person
	 */
	public Boolean edit(Hambur chofer) {
		logger.debug("Editing chofer with id: " + chofer.getid());

		try {
			for (Hambur p : HAMBU) {
				if (p.getid().longValue() == chofer.getid().longValue()) {
					logger.debug("Found record");
					HAMBU.remove(p);
					HAMBU.add(chofer);
					return true;
				}
			}

			logger.debug("No records found");
			return false;

		} catch (Exception e) {
			logger.error(e);
			return false;
		}
	}
}
